package com.topfinance.cnaps2.web.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.topfinance.frame.util.StringUtil;
import com.topfinance.frame.web.BaseSupport;
import com.topfinance.payment.dto.TCfgRouteRuleDto;
import com.topfinance.payment.ebo.TCfgRouteRuleEbo;
import com.topfinance.payment.service.ITCfgRouteRuleMgr;
import com.opensymphony.xwork2.ModelDriven;

/**
 * <p>
 * Description:
 * </p>
 * 
 * @author
 * @version 1.50
 *          <p>
 *          Copyright: Copyright (c) 2004 2005
 *          </p>
 *          <p>
 *          Company:
 *          </p>
 */
public final class CfgRouteAction extends BaseSupport implements ModelDriven<TCfgRouteRuleDto> {

	private ITCfgRouteRuleMgr routeManager = null;

	private TCfgRouteRuleDto routeDto = new TCfgRouteRuleDto();

	public TCfgRouteRuleDto getModel() {
		return routeDto;
	}

	public void setRouteManager(ITCfgRouteRuleMgr routeManager) {
		this.routeManager = routeManager;
	}

	public CfgRouteAction() {
		super();
	}

	public String execute() {
		return list();
	}

	public String list() {
		log.debug("CfgTransAction-list begin");
		try {
			List l = routeManager.getAllRouteRule();
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, l);
			rtnResultMap.put(RECORDS_COUNT, l.size());
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("CfgTransAction-list end");
		return SUCCESS;
	}

	public String create() {
		try {
			log.debug(StringUtil.mapObject(routeDto));
			TCfgRouteRuleEbo o = routeManager.addRoute(routeDto);
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "" + o.getOid();
			rtnResultMap.put("info", getText(CREATE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}

	public String update() {
		try {
			log.debug(StringUtil.mapObject(routeDto));
			routeManager.editRoute(routeDto);
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "";// you can add extra message here.
			rtnResultMap.put("info", getText(UPDATE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}

	public String delete() {
		try {
			String delId = request.getParameter("id");
			routeManager.deleteRoute(new Integer(delId));
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "";// you can add extra message here.
			rtnResultMap.put("info", getText(DELETE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}	

	public TCfgRouteRuleDto getRouteDto() {
		return routeDto;
	}

	public void setRouteDto(TCfgRouteRuleDto routeDto) {
		this.routeDto = routeDto;
	}

	public String query() {
		if (request.getParameter("id") == null) {
			rtnResultMap = new HashMap();
			String[] args = new String[1];
			args[0] = "";// you can add extra message here
			rtnResultMap.put("info", getText("expect.valid.pk.data", args));
			return SUCCESS;
		}
		try {
			String uid = request.getParameter("id");
			System.out.println("[OID]" + uid);
			TCfgRouteRuleEbo trans = routeManager.getRoute(uid);
			List l = new ArrayList<TCfgRouteRuleEbo>();
			l.add(trans);
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, l);
			rtnResultMap.put(RECORDS_COUNT, 1);
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}	

	public String test() {
		log.debug("ConfRoutetion-test begin");
//		try {
//			ConfRouteRuleDto rDto = new ConfRouteRuleDto();
//			rDto.setOpname("测试路径");
//			rDto.setOpseq(10);
//			rDto.setDirection("U");
//			rDto.setOPID(2000);
//			rDto.setIPID(2001);
//		    
//			routeManager.addRoute(rDto);
//			rtnResultMap = new HashMap();
//			rtnResultMap.put("info", getText("create.success", "Test Success!"));
//			return SUCCESS;
//		} catch (Exception e) {
//			handleException(e);
//		}
//		log.debug("ConfRoutetion-list end");
		return SUCCESS;
	}
	
	public String testUpdate() {
		log.debug("ConfRoutetion-test begin");
//		try {
//			ConfRouteRuleDto rDto = new ConfRouteRuleDto();
//			rDto.setUid(1020);
//			rDto.setOpname("测试路径222");
//			rDto.setOpseq(10);
//			rDto.setDirection("U");
//			rDto.setOPID(2000);
//			rDto.setIPID(2001);
//			routeManager.editRoute(rDto);
//			rtnResultMap = new HashMap();
//			rtnResultMap.put("info", getText("create.success", "Test Success!"));
//			return SUCCESS;
//		} catch (Exception e) {
//			handleException(e);
//		}
		log.debug("ConfRoutetion-list end");
		return SUCCESS;
	}
	
}
